package com.haoshuang.zhouzhoubang.ui.page.shop.detail

import com.haoshuang.zhouzhoubang.databinding.ItemGoodsSpecFirstBinding
import com.haoshuang.zhouzhoubang.model.bean.GoodsSpecBean
import com.mufeng.libs.base.BaseBindingHolder
import com.mufeng.libs.base.BaseBindingQuickAdapter
import com.mufeng.libs.core.setOnItemClick
import com.mufeng.libs.utils.view.flexbox


/**
 * 
 */
class GoodsSpecFirstAdapter(data: MutableList<GoodsSpecBean.GoodsSpec>) : BaseBindingQuickAdapter<GoodsSpecBean.GoodsSpec, ItemGoodsSpecFirstBinding>(data) {



    // 规格选中
    var onItemChoose: (() -> Unit)? = null

    override fun onBindViewHolder(
        holder: BaseBindingHolder,
        position: Int,
        item: GoodsSpecBean.GoodsSpec?
    ) {
        holder.getViewBinding<ItemGoodsSpecFirstBinding>().apply {

            tvTitle.text = item?.attr

            val adapter = GoodsSpecSecondAdapter(item?.valueList?.toMutableList() ?: mutableListOf())

            recyclerView.flexbox()
                .adapter = adapter

            adapter.setOnItemClick { view, position, item ->
                val i = adapter.getItem(position)
                adapter.items.forEach {
                    it.isChoose = it.id == i?.id
                }
                adapter.notifyDataSetChanged()
                onItemChoose?.invoke()
            }

        }
    }
}
